class GameUtil{
     //派发事件
	public static dispatchEvent(type:string, obj:Object = null, bubbles:boolean = false, cancelable:boolean = false):void
	{ 	
		var event = new BaseEvent(type,obj,bubbles,cancelable);
		EventManager.getInstance().dispatchEvent(event);
	}

	//监听事件
	public static addEventListener(type:string,listener:Function,thisObject:any=null,useCapture:boolean=false,priority:number=0):void
	{ 
		EventManager.getInstance().addEventListener(type,listener,thisObject,useCapture,priority);
	}

    public static removeEventListener(type:string,listener:Function,thisObject:any=null,useCapture:boolean=false,priority:number=0):void
	{ 
		EventManager.getInstance().removeEventListener(type,listener,thisObject,useCapture);
	}

    /**检测碰撞*/
    public static isHit(obj1: egret.DisplayObject, obj2: egret.DisplayObject): boolean {
        var rect1: egret.Rectangle = obj1.getBounds();
        var rect2: egret.Rectangle = obj2.getBounds();
        rect1.x = obj1.x;
        rect1.y = obj1.y;
        rect2.x = obj2.x;
        rect2.y = obj2.y;
        return rect1.intersects(rect2);
    }

    /**
     * 比较微信基础库版本
     * 返回结果 0表示版本相同
     * 1表示 V1比V2新
     * -1表示 V1比V2老
    */
    public static compareVersion(v1, v2):number{
        v1 = v1.split('.');
        v2 = v2.split('.');
        let len = Math.max(v1.length, v2.length);
        
        while (v1.length < len) {
            v1.push('0');
        }
        while (v2.length < len) {
            v2.push('0');
        }
        
        for (let i = 0; i < len; i++) {
            let num1 = parseInt(v1[i]);
            let num2 = parseInt(v2[i]);
        
            if (num1 > num2) {
                return 1;
            } else if (num1 < num2) {
                return -1;
            }
        }
        return 0;
    }

    public static openAwardUI(awardStr, call?:Function, scope?)
    {
        let data = {
            isPop:true,
            awardStr:awardStr,
            call:call,
            scope:scope
        }
        UIManager.instance().openUILogic(CongratulationsUI, data);
    }

    public static openConfirmUI(scriptData, sureCall?:Function, closeCall?:Function, scope?)
    {
        let data = {
            isPop:true,
            scriptData:scriptData,
            sureCall:sureCall,
            closeCall:closeCall,
            scope:scope
        }
        UIManager.instance().openUILogic(ConfirmUI, data);
    }

    /**
     * 更新红点状态
     */
    public static updateRedPoint(redid:number){
		GameUtil.dispatchEvent(EventConst.UPDATE_REDPOINT, {redid:redid});
	}

    /**
     * 请求红点状态
     */
    public static requsetRedPoint(type_list:string[],call:Function, scope){
		let data = {
            version:GameConst.version,
			type_list:type_list
        }
        HttpManager.instance().sendPost(GameConst.httpUrl + 'warriorother/red_point', StringUtil.jsonToHttpPostString(data), (e)=>{
            let data = JSON.parse(e);
            if(data && data.data)
            {
                let achieve = data.data.achieve;
                if(achieve)
                {
                    RoleCache.instance().isHaveAchievement = achieve;
                }
                if(call)
                {
                    call.call(scope);
                }
            }
        }, this);
	}

    /**
	 * 发送视频，分享次数接口
	 */
	public static sendCntTOSever(type:string)
	{
		let data = {
            version:GameConst.version,
			type:type
        }
		HttpManager.instance().sendPost(GameConst.httpUrl + 'warrior/handle_client_achieve', StringUtil.jsonToHttpPostString(data), null, null);
	}
}